﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder str = new StringBuilder();
            str.AppendLine("Please input the first char to run according program.");
            str.AppendLine("    1.Keep CPU useage at 50%;");
            str.AppendLine("    2.九宫格;");
            str.AppendLine("    3.杨辉三角;");
            //add new function here
            str.AppendLine("    Q.Exit Application.");
            Console.WriteLine(str.ToString());
            System.Threading.Thread newThread = null; ;
        REDO:
            String choose = Console.ReadLine();
            bool iscorrect = true;
            //add new function here
            switch (choose.ToUpper())
            {
                case "1":
                    newThread = new System.Threading.Thread(new System.Threading.ThreadStart(() => KeepCPUUsage(50)));
                    newThread.Start();
                    break;
                case "2":
                    newThread = new System.Threading.Thread(new System.Threading.ThreadStart(() => Grid(11)));
                    newThread.Start();
                    break;
                case "3":
                    newThread = new System.Threading.Thread(new System.Threading.ThreadStart(() => PT(new int[1] { 1 })));
                    newThread.Start();
                    break;
                case "Q":
                    goto EXIT;
                default:
                    iscorrect = false;
                    Console.WriteLine("Please choose correct program...");
                    break;
            }
            if (!iscorrect) goto REDO;

            Console.WriteLine("Press A key to stop previous Thread. Press other key to exit program.");
            ConsoleKeyInfo q = Console.ReadKey();
            if (newThread != null)
                newThread.Abort();
            if (q.Key == ConsoleKey.A)
            {
                Console.WriteLine("Please choose another program...");
                goto REDO;
            }
        EXIT:
            Console.WriteLine("Exit Program.");
        }

        static void KeepCPUUsage(float percent)
        {
            System.Diagnostics.PerformanceCounter p = new System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total");
            while (true)
                if (p.NextValue() > percent)
                    System.Threading.Thread.Sleep(1);

        }
        static void Grid(int n)
        {
            int[,] N = new int[n, n];
            int row = 0, col = n / 2;
            N[row, col] = 1;
            int newRow, newCol;
            for (int i = 2; i <= n * n; i++)
            {
                newRow = (n + row - 1) % n;
                newCol = (col + 1) % n;

                if (N[newRow, newCol] != 0)
                {
                    newRow = row + 1;
                    newCol = col;
                }
                N[newRow, newCol] = i;
                row = newRow;
                col = newCol;
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write("{0,-4}", N[i, j]);
                }
                Console.Write("\r\n");
            }
        }
        static int[] PT(int[] num)
        {
            Console.Write("\n{0}", new string(' ', (10 - num.Length) * 2));
            foreach (int j in num)
            {
                Console.Write("{0,-4}", j);
            }

            if (num.Length == 10)
                return num;
            int[] n = new int[num.Length + 1];
            n[0] = n[n.Length - 1] = 1;
            for (int i = 1; i <= num.Length - 1; i++)
            {
                n[i] = num[i - 1] + num[i];
            }
            return PT(n);
        }
    }
}
